package com.example.demo.practice.dashboardview;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.example.demo.practice.R;
import com.example.demo.practice.innerclass.IBaseActivity;
import com.example.demo.practice.tool.SoundPlay;
import com.example.demo.practice.tool.TimeAction;

import java.util.Random;

public class SmokeView extends IBaseActivity {
    private PanelView mPanelView2;
    private int progress = 10;
    private TimeAction timeAction;
    private MediaPlayer mp;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            setProgress();
            super.handleMessage(msg);
        }
    };

    private void setProgress() {
        progress = 20 + (int) (70 * new Random().nextFloat());
        if(progress > 60){
            SoundPlay soundplay = new SmokeView.SoundTempPlay();
            soundplay.play();
        }
        mPanelView2.setText(Integer.toString(progress)+"%");
        mPanelView2.setPercent(progress);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smokeview);

        mPanelView2 = (PanelView) findViewById(R.id.panelView2);
        mPanelView2.setText(Integer.toString(progress)+"%");
        mPanelView2.setPercent(progress);

        Intent intent = getIntent();
        String code = intent.getStringExtra("code");
        setTitle("烟感" + code);
        String company = intent.getStringExtra("company");
        String address = intent.getStringExtra("address");
        TextView textView1 = (TextView) findViewById(R.id.code);
        code = "编号:" + code;
        textView1.setText(code);
        TextView textView2 = (TextView) findViewById(R.id.company);
        company = "单位:" + company;
        textView2.setText(company);
        TextView textView3 = (TextView) findViewById(R.id.address);
        address = "地址:" + address;
        textView3.setText(address);

        timeAction = new TimeAction(handler, 2000);
        timeAction.start();
    }
    private class SoundTempPlay implements SoundPlay {

        @Override
        public void play() {
            mp = MediaPlayer.create(SmokeView.this, R.raw.smoke);
            mp.start();
        }
    }
    @Override
    protected void onDestroy() {
        timeAction.stop();
        super.onDestroy();
        handler = null;
        mp=null;
        this.finish();
    }

    @Override
    protected void onPause() {
        timeAction.stop();
        super.onPause();
        handler = null;
        mp=null;
        this.finish();
    }

}
